<?php

class Application_Model_DbTable_SupplyType extends Zend_Db_Table_Abstract {

    protected $_name = 'SupplyType';

    public function getSupplyTypeFactor($SupplyTypeId, $supplyId) {      
        $results = array();
        $result = $this->getAdapter()->fetchAssoc("select ID,MainCat,Subcat from RankingRule where SupplyType = ?", array($SupplyTypeId));
        if (count($result) > 0) {
            foreach ($result as $key => $value) {
                $gradeRow = $this->getAdapter()->fetchRow("select RankingGrade from SupplyRanking where SupplyID = ? and RankingRuleID = ?", array($supplyId, $key));

                if (is_array($gradeRow)) {
                    $value["RankingGrade"] = $gradeRow['RankingGrade'];
                } else {
                    $value["RankingGrade"] = "";
                }
                $results[] = $value;
            }
        }        
        $supply = $this->getAdapter()->fetchRow("select Ref,Name,Photo, Comments from Supply where ID = ?", array($supplyId));
        $newval = array(
            'factors' => array_values($results),
        );
        if (is_array($supply)) {
            $newval["Ref"] = $supply['Ref'];
            $newval["Name"] = $supply['Name'];
            $newval["Photo"] = $supply['Photo'];
            $newval["Comments"] = $supply['Comments'];
        } else {
            $newval["Ref"] = "";
            $newval["Name"] = "";
            $newval["Photo"] = "";
            $newval["Comments"] = "";
        }

        return  $newval ;
    }

}